package org.sp.dao.impl;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.sp.dao.IUser;
import org.sp.entity.User;
import org.sp.util.HibernateUtil;

import java.io.Serializable;
import java.util.List;

/**
 * @author 76986
 */
public class UserDao implements IUser {
    /**
     * 获取处理数据的对象session
     */
    private static Session session;
    /**
     * 查询全部用户
     */
    public UserDao(){
      session = HibernateUtil.getInstance().openSession();
    }

//    @Override
//    public List<UserInfo> queryAllUser() {
//        NativeQuery<UserInfo> query = session.createSQLQuery("select * from user_info");
//        query.addEntity(UserInfo.class);
//        return query.list();
//    }

    /**
     * 查询全部用户
     *
     * @return User
     */
    @Override
    public List<User> queryAllUsers() {
        NativeQuery<User> query = session.createSQLQuery("select * from user");
        query.addEntity(User.class);
        return query.list();
    }

    /**
     * 删除用户 by id
     *
     * @param id
     * @return boolean
     */
    @Override
    public Boolean deleteUserById(Integer id) {
        String hql = "delete from User where id = :ids";
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery(hql);
        query.setParameter("ids",id);
        boolean b = query.executeUpdate() > 0;
        transaction.commit();
        return b;
    }

    /**
     * 查询用户信息 by id
     *
     * @param id
     * @return UserInfo
     */
//    @Override
//    public UserInfo queryUserInfoById(Integer id) {
//        String hql = "FROM org.sp.entity.UserInfo u WHERE u.id = :entity_id";
//        Query<UserInfo> query = session.createQuery(hql);
//        query.setParameter("entity_id", id);
//        List<UserInfo> list = query.list();
//        if (list.size() > 0){
//            return list.get(0);
//        }
//        return null;
//    }

    /**
     * 删除用户信息 by id
     *
     * @param id
     * @return
     */
//    @Override
//    public Boolean deleteUserInfoById(Integer id) {
//        String sql = "DELETE FROM org.sp.entity.UserInfo WHERE id = :ids";
//        Transaction transaction = session.beginTransaction();
//        Query query = session.createQuery(sql);
//        query.setParameter("ids",id);
//        boolean b = query.executeUpdate() > 0;
//        transaction.commit();
//        return b;
//    }

    /**
     * 修改用户信息 by id
     *
     * @param userInfo
     * @return boolean
     */
//    @Override
//    public Boolean updateUserInfoById(UserInfo userInfo) {
//        String sql = "update UserInfo set name = :name where id =:id";
//        session.beginTransaction();
//        Query query = session.createQuery(sql);
//        query.setParameter("name",userInfo.getName());
//        query.setParameter("id",userInfo.getId());
//        return query.executeUpdate() > 0;
//    }

    /**
     * 插入数据
     *
     * @param user
     * @return boolean
     */
//    @Override
//    public Boolean insertUser(User user) {
//        Transaction transaction = session.beginTransaction();
//        Serializable save = session.save(user);
//        if (save != null) {
//            transaction.commit();
//            return true;
//        }
//        return false;
//    }
}
